package com.errol.generic.log;

import com.renjia.mq.service.MsgCenterDingRobotService;
import com.renjia.properties.UkerServerProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * lombok @Slf4j 实现
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2018/04/20
 **/
@Slf4j
@Service
public class Slf4jServiceImpl implements LogService {

    private final UkerServerProperties ukerServerProperties;
    private final MsgCenterDingRobotService msgCenterDingRobotService;

    public Slf4jServiceImpl(UkerServerProperties ukerServerProperties,
                            MsgCenterDingRobotService msgCenterDingRobotService) {
        this.ukerServerProperties = ukerServerProperties;
        this.msgCenterDingRobotService = msgCenterDingRobotService;
    }

    private void tryRemind(String s) {
        if (ukerServerProperties.isProd() || ukerServerProperties.isQa()) {
            msgCenterDingRobotService.sendMsgToDingRobot(s);
        }
    }

    private boolean isProd() {
        return ukerServerProperties.isProd();
    }

    @Override
    public String remind(String desc) {
        log.warn(desc);
        tryRemind(desc);
        return desc;
    }

    @Override
    public String remind(String format, Object... args) {
        String s = String.format(format, args);
        log.warn(s);
        tryRemind(s);
        return s;
    }

    @Override
    public String remind(List<String> desc) {
        String join = String.join("\n", desc);
        log.warn(join);
        tryRemind(join);
        return join;
    }

    @Override
    public String debug(String desc) {
        if (isProd()) {
            return desc;
        }
        log.info(desc);
        return desc;
    }

    @Override
    public String debug(String format, Object... args) {
        String s = String.format(format, args);
        if (isProd()) {
            return s;
        }
        log.info(s);
        return s;
    }

    @Override
    public String debug(List<String> desc) {
        String join = String.join("\n", desc);
        if (isProd()) {
            return join;
        }
        log.info(join);
        return join;
    }

    @Override
    public String info(String desc) {
        log.info(desc);
        return desc;
    }

    @Override
    public String info(String format, Object... args) {
        String s = String.format(format, args);
        log.info(s);
        return s;
    }

    @Override
    public String info(List<String> desc) {
        String join = String.join("\n", desc);
        log.info(join);
        return join;
    }

    @Override
    public String warn(String desc) {
        log.warn(desc);
        return desc;
    }

    @Override
    public String warn(String format, Object... args) {
        String s = String.format(format, args);
        log.warn(s);
        return s;
    }

    @Override
    public String warn(Throwable e) {
        log.warn(e.getMessage(), e);
        return e.getMessage();
    }

    @Override
    public String warn(Throwable e, String desc) {
        log.warn(desc);
        log.warn(e.getMessage(), e);
        return desc;
    }

    @Override
    public String error(Throwable e) {
        log.error(e.getMessage(), e);
        return e.getMessage();
    }

    @Override
    public String error(Throwable e, String desc) {
        log.error(desc);
        log.error(e.getMessage(), e);
        return desc;
    }

}
